package com.lagou.edu.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 日志增强逻辑
 */
@Component
@Aspect
public class LogUtil {

    @Pointcut("execution(* com.lagou.edu.service.impl.*.*(..))")
    public void pointcut(){}

    /**
     * 前置通知
     */
    @Before("pointcut()")
    public void beforeMethod(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("beforeMethod, fromCardNo:"+args[0]+" toCardNo:"+args[1]+" money:" +args[2] +"....");
    }

    /**
     * 最终通知
     */
    @After("pointcut()")
    public void afterMethod() {
        System.out.println("afterMethod....");
    }

    /**
     * 异常通知
     */
    @AfterThrowing("pointcut()")
    public void exceptionMethod() {
        System.out.println("exceptionMethod....");
    }

    /**
     * 正常执行时通知
     */
    @AfterReturning("pointcut()")
    public void returnMethod() {
        System.out.println("returnMethod....");
    }

    /**
     * 环绕通知
     */
    //@Around("pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        System.out.println("环绕通知 before....");
        Object result = null;
        try {
            // 控制原有业务逻辑是否执行
            result = joinPoint.proceed(joinPoint.getArgs());
        } catch (Throwable throwable) {
            System.out.println("环绕通知 exception....");
        }
        System.out.println("环绕通知 after....");
        return result;
    }
}
